import AlipaySdk from 'alipay-sdk';
import AliPayFormData from 'alipay-sdk/lib/form'
import { readFileSync } from 'fs';
import { join } from 'path';
export class AliConfig {
    private appid = '2021003139604986'
    private AES = 'fVHWls6Vb51UuCrd/KP4nw=='
    private privateKey = readFileSync(join(__dirname, '../../src/pay', 'ali-private-key.pem'), 'ascii')
    private alipayPublicKey =  readFileSync(join(__dirname, '../../src/pay', 'ali-public-key.pem'), 'ascii')
    private gateway = 'https://openapi.alipay.com/gateway.do'
    // 沙箱配置start
    // private gateway = 'https://openapi.alipaydev.com/gateway.do'
    // private appid = '2021000121642524'
    // private AES = 't5M+MwHv1ikc1W3B6edC7A=='
    // private privateKey = readFileSync(join(__dirname, '../../src/pay', 'ali-sandbox-private-key.pem'), 'ascii')
    // private alipayPublicKey = readFileSync(join(__dirname, '../../src/pay', 'ali-sandbox-public-key.pem'), 'ascii')
    // 沙箱配置end
    public aliPay: AlipaySdk;
    constructor() {
        this.aliPay = this.createAliPaySdc();
    }
    private createAliPaySdc() {
        return new AlipaySdk({
            // 参考下方 SDK 配置
            appId: this.appid,
            privateKey: this.privateKey,
            //可设置AES密钥，调用AES加解密相关接口时需要（可选）
            encryptKey: this.AES,
            gateway: this.gateway,
            alipayPublicKey: this.alipayPublicKey
        });
    }
    async pagePay(param: {
        outTradeNo: string
        totalAmount: number
        subject: string
    }) {
        const formData = new AliPayFormData();
        formData.setMethod('get');
        formData.addField('notifyUrl', 'https://bangkeedu.com:3000/payNotice/ali');
        formData.addField('returnUrl', 'https://bangkeedu.com:3000/payNotice');
        formData.addField('bizContent', {
            ...param,
            qr_pay_mode:'4',
            productCode: 'FAST_INSTANT_TRADE_PAY',
            qrcodeWidth:148
        });

        const result = await this.aliPay.exec('alipay.trade.page.pay', {}, { formData });
        console.log(result);
        return result

    } 

}